${deferlight}

${shadow}

${fog}

uniform ece_LightSource light[MaxLightCount];
uniform ece_ShadowMapping shadow;
uniform ece_Fog fog;
uniform sampler2D eceGlPosition;
uniform sampler2D eceGlNormal;
uniform sampler2D eceGlAlbedoSpec;
uniform sampler2D eceDecal;
uniform vec3 viewPosition;
varying vec2 vTexCoord;
void main() {
	vec4 ocolor = texture2D(eceGlAlbedoSpec, vTexCoord);
	vec3 odiffuse = ocolor.rgb;
	float ospecular = ocolor.a;
	if(ospecular < 0) {
		gl_FragColor = vec4(odiffuse, 1.0);
	} else {
		vec4 temp = texture2D(eceGlNormal, vTexCoord);
		if((abs(temp.r) + abs(temp.g) + abs(temp.b)) > 0) {
			vec3 normal = normalize(temp.rgb);
			float shininess = temp.a;
			
			vec4 decal = texture2D(eceDecal, vTexCoord);
			vec3 baseColor = mix(odiffuse, decal.rgb, decal.a);
			
			temp = texture2D(eceGlPosition, vTexCoord);
			vec3 vPosition = temp.rgb;
			float vDistance = temp.a;
			
			vec3 viewDir = normalize(viewPosition - vPosition);
				
			vec3 ambient = vec3(0);
			vec3 diffuse = vec3(0);
			vec3 specular = vec3(0);
			
			LightPixel(light, vPosition, normal, viewDir, shininess, ambient, diffuse, specular);
			
		    float shadowResult = 0;
		    if(shadow.enable) {
		    	vec4 vShadowCoord = shadow.shadowMatrix * vec4(vPosition, 1.0); 
		    	vec3 lightDir = normalize(light[shadow.lightIndex].position.xyz - vPosition);
				shadowResult = calculateShadow(shadow.shadowMapping, vShadowCoord, lightDir, normal);
		    }
		    vec3 color = ((ambient + (1.0 - shadowResult) * (diffuse + specular * ospecular)) * baseColor);
		    FogMix(fog, vDistance, color);
		    gl_FragColor = vec4(color, 1.0);
		} else {
			discard;
		}
	}
}